<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {
    public $con, $ac, $md;
    public $mid,$item,$f,$tb;
    public function _initialize()
    {
        $this->ac = ACTION_NAME;
        $this->con = CONTROLLER_NAME;
        $this->md = CONTROLLER_NAME;
        $this->assign('md', $this->md);
        $this->assign('con', $this->con);
        $this->assign('ac', $this->ac);


        $this->assign('addUrl', U($this->con.'/add'));
        $this->assign('editUrl', U($this->con.'/edit?id='.I('id')));
        $this->assign('viewUrl', U($this->con.'/view?id='.I('id')));
        $this->assign('deleteUrl', U($this->con.'/delete?id='.I('id')));
        $this->assign('backUrl', U($this->con.'/index'));

        $this->assign('insertUrl', U($this->con.'/insert'));
        $this->assign('updateUrl', U($this->con.'/update'));
        $this->mid = session('mid');

        if (!$this->mid) {
            $this->display('Public/login');
            exit();
        }

        

        $this->f = $_GET['f'];
        $this->assign('f', $this->f);


        if ($this->f) {
            $this->item = F(C('ITEM_DIR').$this->f);
            foreach ($this->item as $key => $value) {
                C($key,$value);
            }
        }


        $this->tb = I('tb');
        $this->assign('tb', $this->tb);

        $this->getItems();

        layout(true);
        if (array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX'])
        {
            layout(false);
        }
    }


    public function getItems()
    {
        $item_files = scandir(APP_PATH.'Runtime/data/'.C('ITEM_DIR'));
        $itmes = array();
        foreach ($item_files as $k) {
            if ($k == '.' || $k == '..') continue;
            $k = str_replace(".php", "", $k);
            $items[] = F(C('ITEM_DIR').$k);

            if (I('f') == $k) {
                $this->item = F(C('ITEM_DIR').$k);
                $this->assign('item', $this->item);
                foreach ($this->item as $key => $value) {
                    C($key,$value);
                }
                $this->getTables();
            }
        }
        $this->assign('items', $items);
        return $items;
    }

    public function getTables()
    {
        $Model = M();
        $tables = $Model->query("select * from information_schema.tables where table_schema='".C('DB_NAME')."' and table_type='base table';");
        $this->assign('tables', $tables);
        return $tables;
    }


    public function ckDbConnect($DB_HOST ='',$DB_USER ='',$DB_PWD ='',$DB_NAME ='')
    {
        if (!$DB_HOST) $DB_HOST = $this->item['DB_HOST'];
        if (!$DB_USER) $DB_USER = $this->item['DB_USER'];
        if (!$DB_PWD) $DB_PWD = $this->item['DB_PWD'];
        if (!$DB_NAME) $DB_NAME = $this->item['DB_NAME'];


        if (!$DB_HOST || !$DB_USER || !$DB_PWD) {
            $dbmsg = '请正确填写数据库连接参数';
        }
        $conn = @mysql_connect($DB_HOST,$DB_USER,$DB_PWD);
        if (!$conn) {
            $dbmsg =  '数据库连接错误，请配置数据库连接参数';
        } else {
            $db = mysql_select_db($DB_NAME,$conn);
            if (!$db) {
                $dbmsg =  '选择数据库错误，请重新配置数据库名';
            }
        }
        $this->assign('dbError', $dbmsg);
    }
}
